<?php

/**
 * 
 */
class Thumbs_model extends CI_Model {

    public function __construct() {
        parent::__construct();
        $this->load->database();
    }

    public function fetchThumbs() {
        $query = $this->db->get('thumbs');
        return $query->result_array();
    }

    public function fetchThumbById($id) {
        $sql = "select decision,count(decision) as count from thumbs where report_id='$id' 
		group by decision order by decision asc"; // musime zachovat poradie kvoli vyberu v controlleri
        $query = $this->db->query($sql);
        return $query->result_array();
    }

    public function insertThumb($decision, $id, $user_id) {


        if (!$this->hasVoted($user_id, $id)) {
            $date = date('Y-m-d H:i:s');
            $sql = "INSERT into thumbs VALUES('','$user_id','$id','$date','$decision')";
            $query = $this->db->query($sql);
            return 'ok';
        } else {
            return 'voted';
        }
    }

    public function hasVoted($user_id, $id) {
        $sql_has_voted = "SELECT decision from thumbs where report_id='$id' and user_id='$user_id'";
        $query = $this->db->query($sql_has_voted);
        return $query->row_array();
    }

}

?>